package tantan.tantanxposed;

import android.content.Context;

import com.google.gson.Gson;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

import static de.robv.android.xposed.XposedHelpers.callMethod;
import static de.robv.android.xposed.XposedHelpers.callStaticMethod;
import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
import static de.robv.android.xposed.XposedHelpers.findClass;

/**
 * Created by xuxingcan on 16-10-23.
 */
@SuppressWarnings("ResourceType")
public class XposedInit implements IXposedHookLoadPackage {
    private Class<?> BDLocation;
    private Gson gson;

    @Override
    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
        String packageName = loadPackageParam.packageName;
        if (!packageName.equalsIgnoreCase("com.p1.mobile.putong")) {
            return;
        }
        ClassLoader classLoader = loadPackageParam.classLoader;
        try {
            Object activityThread = callStaticMethod(findClass("android.app.ActivityThread", null), "currentActivityThread");
            Context mContext = (Context) callMethod(activityThread, "getSystemContext");
            gson = new Gson();
            try {
                String versionName = mContext.getPackageManager().getPackageInfo(packageName, 0).versionName;
                XposedBridge.log("Loaded app: " + loadPackageParam.packageName + " " + versionName);
            } catch (Exception e) {
                XposedBridge.log(e);
            }
            BDLocation = findClass("com.baidu.location.BDLocation", classLoader);
            findAndHookMethod("l.Hu", classLoader, "onReceiveLocation", BDLocation, new XC_MethodHook() {
                @Override
                protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                    super.beforeHookedMethod(param);
                    XposedBridge.log("onReceiveLocation");
                }
            });
            findAndHookMethod(BDLocation, "getLatitude", new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    super.afterHookedMethod(param);
                    XposedBridge.log("getLatitude");
                    double d = 29.4874481951;
                    param.setResult(d);
                }
            });
            findAndHookMethod(BDLocation, "getLongitude", new XC_MethodHook() {
                @Override
                protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                    super.afterHookedMethod(param);
                    XposedBridge.log("getLongitude");
                    double d = 106.5449931755;
                    param.setResult(d);
                }
            });
        } catch (Exception e) {
            XposedBridge.log(e);
        }
    }
}